﻿using MySqlX.XDevAPI.Common;
using Publicuse.Appication;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Application.system
{
    public class DowntaskApplication
    {
        private static BaesApplication baseApplication;
        private static DowntaskApplication userApplication;

        public static DowntaskApplication GetApplication(ServiceContainer container)
        {
            baseApplication = new BaesApplication(container);
            return userApplication;
        }

        static DowntaskApplication()
        {
            userApplication = new DowntaskApplication();
        }

        /// 获取系统生成下载任务信息
        /// </summary>
        /// <returns></returns>
        public async Task<CommonResult> GetList(Downtask query)
        {
            return await baseApplication.GetListByExpression<Downtask>(query, query.pageIndex, query.pageSize, o => o.Id, false, expression =>
            {
                var LoginAppid = HttpContextExtendBase.GetLoginAppid();
                query.loginUserCode = HttpContextExtendBase.GetLoginCode();
                if (string.Compare(LoginAppid, "admin", true) != 0 && string.Compare(query.loginUserCode, "ajng", true) != 0 && string.Compare(query.loginUserCode, "sysadmin", true) != 0 && string.Compare(query.loginUserCode, "jtfz", true) != 0)
                {
                    expression = expression.And(o => o.fucode == query.loginUserCode);
                }
                if (!string.IsNullOrEmpty(query.sttime))
                {
                    expression = expression.And(o => Convert.ToDateTime(o.cTime) >= Convert.ToDateTime(query.sttime));
                }
                if (!string.IsNullOrEmpty(query.ettime))
                {
                    expression = expression.And(o => Convert.ToDateTime(o.cTime) <= Convert.ToDateTime(query.ettime));
                }
                return expression;
            });
        }

        /// <summary>
        /// 删除系统生成下载任务信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<CommonResult> Delete(string Id, string loginUserCode, string type)
        {
            return await Task.Factory.StartNew(() =>
            {
                CommonResult result = new CommonResult();
                string msg = string.Empty;
                string sql = string.Empty;
                var table = TableAtributeUtil.GetTable<Downtask>();
                var list = baseApplication.mySqlSugar.QueryByExpression<Downtask>(o => o.Id == Id.GetIntValue(), out msg);
                if(string.IsNullOrEmpty(msg) && list!=null && list.Count > 0)
                {
                    var curdata = list.FirstOrDefault();
                    if (curdata.status==1)
                    {
                        sql = $"update {table} set status=-9 where Id=@Id;";
                    }
                    else
                    {
                        curdata.furl = EnDecryptionUtil.DESDecryptbyqd(curdata.furl);
                        var filepath = baseApplication.webHost.WebRootPath + curdata.furl;
                        if (FileUtil.Exists(filepath))
                        {
                            FileUtil.DelFile(filepath);
                        }
                        sql = $"delete from  {table}  where Id=@Id;";
                    }
                    baseApplication.mySqlSugar.ExecuteSql(sql, new { Id = Id }, out msg);
                    result.msg = msg;
                }
                else
                {
                    result.msg = "数据不存在！";
                }
                return result;
            });
        }

        /// <summary>
        /// 还原路系统生成下载任务信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        public async Task<CommonResult> Reduction(string Id, string loginUserCode)
        {
            return await baseApplication.Reduction<Downtask>(Id, loginUserCode, BusinessType.dowtask, (a, b, info) =>
            {
                return info.Id.ToString();
            });
        }
    }
}
